package com.fooddiary.dao.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.fooddiary.dao.IUserDao;
import com.fooddiary.model.User;

/**
 * 
 * @author Created by andrey.kuznetsov <br>
 * @author Last modified by $Author$ <br>
 * @author Last modified on $Date$ at revision $Revision$ <br>
 */
@Repository
public class UserDao extends BaseDao implements IUserDao {

	private final String PROPERTY_LOGIN = "login";

	@SuppressWarnings("unchecked")
	@Override
	public User getUserByLogin(String p_login) {
		if(StringUtils.isEmpty( p_login )){
			return null;
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class)
		.add(Restrictions.eq(PROPERTY_LOGIN, StringUtils.trim( p_login )));
		List<User> users = hibernateTemplate.findByCriteria(criteria);
		return CollectionUtils.isNotEmpty( users ) ? users.get( 0 ) : null;
	}

}
